package com.sxhuayuan.parking.service.impl;

import com.sxhuayuan.parking.compenent.Message;
import com.sxhuayuan.parking.compenent.shiro.AccountType;
import com.sxhuayuan.parking.compenent.shiro.Principal;
import com.sxhuayuan.parking.compenent.web.RespMessage;
import com.sxhuayuan.parking.dao.BaseDao;
import com.sxhuayuan.parking.dao.SalesmanDao;
import com.sxhuayuan.parking.entity.Salesman;
import com.sxhuayuan.parking.exception.MyException;
import com.sxhuayuan.parking.service.SalesmanService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.Objects;

/**
 * 业务员 服务实现类
 * @author helong
 * @create 2019-08-02 11:56
 */
@Service("salesmanServiceImpl")
public class SalesmanServiceImpl extends BaseServiceImpl<Salesman,Long> implements SalesmanService {

    @Autowired
    SalesmanDao dao;

    @Override
    public BaseDao<Salesman, Long> getDAO() {
        return dao;
    }

    @Transactional(readOnly = true)
    public boolean usernameExists(String username) {
        return dao.count((Root<Salesman> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) -> criteriaBuilder.equal(root.get("username"), username) ) > 0;
    }

    @Transactional(readOnly = true)
    public Salesman findByUsername(String username) {
        return dao.findByUsername(username);
    }

    @Transactional(readOnly = true)
    public boolean isAuthenticated() {
        Subject subject = SecurityUtils.getSubject();
        if (subject != null && subject.getPrincipal() != null) {
            Principal principal = (Principal) subject.getPrincipal();
            if (principal.getType() == AccountType.salesman) {
                return subject.isAuthenticated();
            }
        }
        return false;
    }

    @Transactional(readOnly = true)
    public Salesman getCurrent() {
        Subject subject = SecurityUtils.getSubject();
        if (subject != null && subject.getPrincipal() != null) {
            Principal principal = (Principal) subject.getPrincipal();
            if (principal.getType() == AccountType.salesman) {
                return dao.findById(principal.getId()).get();
            }
        }
        return null;

    }

    @Override
    @Transactional
    @CacheEvict(value = { "salesmanAuthorization", "commonData" }, allEntries = true)
    public Salesman update(Salesman salesman) {
        if (Objects.isNull(salesman.getId())) {
            throw new MyException( RespMessage.Status.VALID_ERR, Message.FORM_VALID_FAIL);
        }
        try {
            return super.update(salesman);
        } catch (org.springframework.dao.DataIntegrityViolationException e) {
            throw new MyException( RespMessage.Status.ERROR, Message.FORM_USERNAME_EXISTS);
        }
    }

}
